import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Map;
import java.util.Date;


public class JShell {
	
	String commandTemp = "";
	String input;
	
	ArrayList<String> command = new ArrayList<String>();
	ArrayList<ArrayList<String>> history = new ArrayList<ArrayList<String>>();
	ArrayList<Timestamp> timeList = new ArrayList<Timestamp>();
	Map<String, String> env;
	
	
	BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
	ProcessBuilder pb;
	Date date= new java.util.Date();
	Timestamp timeStamp;
	
	String lastLog;
	
	@SuppressWarnings("unchecked")
	public void main() throws IOException
	{
		pb = new ProcessBuilder();
		pb.directory(new File(System.getProperty("user.dir")));
		
		timeStamp = new Timestamp(date.getTime());
		System.out.println("---------------|JSHELL|------------------");
		System.out.println("Logged in at: " + timeStamp.toString());
		System.out.println("Last log in : " + loadStamp());
		while(true)
		{
			
			System.out.print("JSH ==> ");
			input = console.readLine();
			timeStamp = new Timestamp(date.getTime());
			
			parseText(input);
			
			if(command.get(0).compareTo("cd") == 0)
			{
				cdCommand();
			}
			if(command.get(0).compareTo("ls") == 0)
			{
				lsCommand();
			}
			if(command.get(0).compareTo("hist") == 0)
			{
				histCommand();
			}
			if(command.get(0).compareTo("exit") == 0)
			{
				timeStamp = new Timestamp(date.getTime());
				System.out.println("Logged out at: " + timeStamp.toString());
				saveStamp(timeStamp);
				System.exit(0);
			}
			
			history.add((ArrayList<String>) command.clone());
			timeList.add((Timestamp) timeStamp.clone());
			command.clear();
		}
	
	}
	
	private void saveStamp(Timestamp timeStamp) 
	{
		try{
	    	
			File file = new File("time.txt");
			file.delete();
			file = new File("time.txt");
			file.createNewFile();
			
			Writer output = null;
			output = new BufferedWriter(new FileWriter(file));
		    output.write(timeStamp.toString());
		    
		    output.close();

		    }catch (Exception e){ System.err.println("Error: " + e.getMessage());}

		
	}

	private String loadStamp()
	{
		try
		{
		FileInputStream fstream = new FileInputStream("time.txt");
	    DataInputStream in = new DataInputStream(fstream);
	    BufferedReader br = new BufferedReader(new InputStreamReader(in));
	    
	    //Read File Line By Line
	    lastLog = br.readLine();
	    
	    in.close();
	    if(lastLog != null)
	    {
	    	return lastLog;
	    }
	    else
	    {
	    	return "Never";
	    }
	    
	    
	    }catch (Exception e){ System.err.println("Error: " + e.getMessage());}
		
	    return lastLog;
		
		
	}
	private void histCommand() {
		
		System.out.println("Commands entered");
		for(int i = 0; i < history.size(); i++)
		{
			ArrayList<String> list = history.get(i);
			
			for(int j = 0; j < list.size(); j++)
			{
				System.out.print(list.get(j));
			}
			
			System.out.print( " @ " + timeList.get(i));
			System.out.println();
		}
		
	}

	public void lsCommand()
	{
		String[] roots = pb.directory().list();
		
		System.out.println(pb.directory());
		System.out.print("-> | ");
		for(int i = 0; i < roots.length; i ++)
		{
			System.out.print(roots[i] + " | ");
		}
		System.out.println();
	}
	
	public void cdCommand()
	{
		File path = pb.directory();
		env = pb.environment();
		
		
		switch(command.get(1).charAt(0))
		{
		case '-' : cdGoBack(path); break;
		case '\\': 
			if(command.get(1).charAt(0) == '\\')
			{
				path = new File(path.toString().concat("\\" + command.get(1)));
				pb.directory(path);
			}
			break;
			
		default: 
			
			String newPath = command.get(1);
			
			for(int i = 1; i < command.size(); i ++)
			{
				if(command.get(i).charAt(0) != '\\')
				{
					newPath.concat(" " + command.get(i));
				}
				if(command.get(i).charAt(0) == '\\')
				{
					newPath.concat(command.get(i));
				}
			}
			
			System.out.println(newPath);
			//pb.directory(new File(newPath)); break;
		}
	}
	
	private void cdGoBack(File path)
	{
		if(command.get(1).compareTo("-b") == 0)
		{
			int n = 1;
			for(int i = 2; i < command.size(); i++)
			{
				if(command.get(i).compareTo("-b") == 0) { n++; }
			}
			
			path = pb.directory();
			String s = path.toString();
			
			int pointer = 0;
			//find last \
			if(n == 1)
			{
				for( int i = 0; i < s.length(); i++)
				{
					if(s.charAt(i) == '\\')
					{
						pointer = i;
					}
				}
				s = s.substring(0, pointer);
				System.out.println(s);
				pb.directory(path = new File(s));
			}
			//For multiple -b
			if(n > 1)
			{
				for( int i = 0; i < n; i++)
				{
					for(int j = 0; j < s.length() ; j++)
					{
						if(s.charAt(j) == '\\')
						{
							pointer = j;
						}
					}
					s = s.substring(0, pointer);
				}
				pb.directory(path = new File(s = s.substring(0, pointer)));
			}
			
		}
	}

	private ArrayList<String> parseText(String c) 
	{
		String s;
		int pointer = 0;
		for(int i = 0; i < c.length(); i++)
		{
			if(c.charAt(i) == ' ')
			{
				s = c.substring(pointer,i);
				pointer = i + 1;
				command.add(s);
			}
			if(i == c.length() -1)
			{	
				s = c.substring(pointer, c.length());
				command.add(s);
			}
		}
		return command;
	}
}



